﻿using Ovvio.Data;
using System.Linq;
using System;
using System.Security.Principal;
using System.Collections.Generic;


namespace Ovvio.Business
{
    public enum Role
    {
        Guest = 0, Member = 1, Admin = 2
    }
    public class AuthenticationService 
    {
        public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            using (var ctx = new OvvioDataContext())
            {
                var query = ctx.Users.Where(p => p.Username.ToUpper().Equals(username.ToUpper()));

                if (query.Any())
                {
                    var user = query.First();
                    if (user.DecryptPassword().Equals(oldPassword))
                    {
                        user.SetPassword(newPassword);
                        return ctx.SaveChanges() > 0;
                    }
                }

            }
            return false;
        }

        public static UserPrincipal SignIn(string username)
        {
            using (var ctx = new OvvioDataContext())
            {
                var query = ctx.Users.Where(p => p.Username.ToUpper().Equals(username.ToUpper()));

                if (query.Any())
                {
                    var user = query.First();
                    user.LastLogonDate = DateTime.Now;
                    ctx.SaveChanges();
                    var identity = new UserIdentity(true, user.Id,user.Username, user.FirstName);
                    return new UserPrincipal(identity, (Role)user.Role);
                }
            }
            return null;
        }

        public bool ValidateUser(string username, string password)
        {

            //if (username.ToUpper() == "ADMINISTRATOR")
            //{
            //    var service = new UserService();

            //    var entity = service.GetEntity(p => p.Username.ToUpper() == "ADMINISTRATOR");

            //    if (entity != null)
            //        service.Delete(entity, entity.Id);

            //    var newEntity = service.Create();
            //    newEntity.Username = "ADMINISTRATOR";
            //    newEntity.FirstName = "John";
            //    newEntity.LastName = "Smith";
            //    newEntity.Mobile = "01234567";
            //    newEntity.IsActive = true;
            //    newEntity.UpdatedBy = "ADMINISTRATOR";
            //    newEntity.RegistrationDate = DateTime.Now;
            //    newEntity.IsNewsLetterSubscriber = true;
            //    newEntity.Email = "administrator@ovvio.com";
            //    newEntity.Role = 2;
            //    newEntity.SetPassword("admin");
            //    service.Save(newEntity, -1);
            //}
            using (var ctx = new OvvioDataContext())
            {
                var query = ctx.Users.Where(p => p.Username.ToUpper().Equals(username.ToUpper()));

                if (query.Any())
                    return query.First().DecryptPassword().Equals(password);
            }
            return false;
        }
    }
}
